from django.db import models

# Create your models here.
from DjangoUeditor.models import UEditorField
from users.models import UserProfile


class Post(models.Model):
    title = models.CharField(max_length=50, verbose_name="标题")
    author = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name='posts', verbose_name="作者")
    content = UEditorField(imagePath="post/images/", width=1000, height=300,
                           filePath="post/files/", default='', verbose_name="内容")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="发布时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    delete_time = models.DateTimeField(blank=True, null=True, verbose_name="删除时间")
    is_delete = models.NullBooleanField(blank=True, null=True, verbose_name="删除")
    click_count = models.IntegerField(null=True, default=0, verbose_name="点击量")
    praise_count = models.IntegerField(null=True, default=0, verbose_name="点赞量")
    comment_count = models.IntegerField(null=True, default=0, verbose_name="评论量")
    collect_count = models.IntegerField(null=True, default=0, verbose_name="收藏量")
    inform_count = models.IntegerField(null=True, default=0, verbose_name="举报量")

    class Meta:
        db_table = 'post'
        verbose_name = '动态'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class PostImages(models.Model):
    post = models.ForeignKey(Post, models.DO_NOTHING, related_name="images", db_column='post', blank=True, null=True,
                             verbose_name="动态")
    image = models.ImageField(max_length=150, upload_to='post_images/', blank=True, null=True, verbose_name="图片")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    delete_time = models.DateTimeField(blank=True, null=True, verbose_name="删除时间")
    is_delete = models.NullBooleanField(blank=True, null=True, verbose_name="删除")

    class Meta:
        db_table = 'post_images'
        verbose_name = '动态图片'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.image.name
 


class PraisePost(models.Model):
    user = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name="post_praises", blank=True, null=True,
                             verbose_name="点赞者")
    post = models.ForeignKey(Post, models.DO_NOTHING, related_name="post_praises", blank=True, null=True,
                             verbose_name="动态")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")

    class Meta:
        db_table = 'praise_post'
        unique_together = (('user', 'post'),)
        verbose_name = '点赞动态'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.name}点赞了{self.post.title}'


class CollectPost(models.Model):
    user = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name="collect_posts", blank=True, null=True,
                             verbose_name="收藏者")
    post = models.ForeignKey(Post, models.DO_NOTHING, related_name="collect_posts", blank=True, null=True,
                             verbose_name="动态")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")

    class Meta:
        db_table = 'collect_post'
        unique_together = (('user', 'post'),)
        verbose_name = '收藏动态'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.name}收藏了{self.post.title}'


class CommentPost(models.Model):
    post = models.ForeignKey(Post, models.DO_NOTHING, related_name="comment_posts", blank=True, null=True,
                             verbose_name="动态")
    user = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name="comment_posts", blank=True, null=True,
                             verbose_name="评论者")
    pid = models.ForeignKey('self', related_name="comments", null=True)
    content = models.CharField(max_length=250, blank=True, null=True, verbose_name="内容")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="发表时间")
    is_delete = models.NullBooleanField(blank=True, null=True, verbose_name="已删除")

    class Meta:
        db_table = 'comment_post'
        verbose_name = '评论动态'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.name}评论了{self.post.title}'


class InformPost(models.Model):
    user = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name="inform_posts", blank=True, null=True,
                             verbose_name="举报者")
    post = models.ForeignKey(Post, models.DO_NOTHING, related_name="inform_posts", blank=True, null=True,
                             verbose_name="动态")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="举报时间")

    class Meta:
        db_table = 'inform_post'
        unique_together = (('user', 'post'),)
        verbose_name = '举报动态'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.name}举报了{self.post.title}'
